<template>
  <div class="result">
    <van-nav-bar
      :title="keyWord + '的搜索结果'"
      left-arrow
      @click-left="$router.back()"
      fixed
    />
    <van-list
      v-model="loading"
      :finished="finished"
      finished-text="没有更多了"
      @load="onLoad"
    >
      <van-cell
        v-for="(item,index) in list"
        :key="index"
        :title="item.title"
        @click="$router.push('/article/' + item.art_id)"
      />
    </van-list>
  </div>
</template>

<script>
import { getSearchResult } from '@/api/search'
export default {
  name: 'SearchResult',
  computed: {
    keyWord () {
      return this.$route.query.keyword
    }
  },
  data () {
    return {
      loading: false,
      finished: false,
      list: [],
      page: 1
    }
  },
  methods: {
    async onLoad () {
      const res = await getSearchResult(this.keyWord, this.page)
      this.list = [...this.list, ...res.data.results]
      this.loading = false
      this.page++
      if (res.data.results.length === 0 || this.list.length >= 50) {
        this.finished = true
      }
    }
  }
}
</script>

<style lang="less" scoped>
.result{
  padding-top: 46px;
  height: 100vh;
  overflow: hidden;
  .van-list{
    height: 100%;
    overflow: auto;
  }
}
</style>
